/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * An interface object for the game mode which contains almost everything
 */

#include "GameInterfaceGameContainer.h"
#include "GameInterfacePhysicObjectButton.h"
#include "../GameScene.h"
#include "../GameEngine.h"
#include "../layers/LayerGame.h"
#include "../objects/physicobjects/GamePhysicObjectEngine.h"
#include "../objects/physicobjects/bombs/GameBomb.h"
#include "../objects/physicobjects/borders/GameBorder.h"
#include "../misc/GameWorldModule.h"
#include "../../Common.h"
#include "../../Resources.h"

using namespace cocos2d;

// default constructor
GameInterfaceGameContainer::GameInterfaceGameContainer( cocos2d::CCLayer *layer, MainEngine *mainEngine, GameScene *gameScene )
	: GameInterfaceItemAndObjectContainer( layer, mainEngine ), mGameScene( gameScene ), mShouldCausePhysicsExplosion( false ) {

	// create bomb items

	// small mine
	GameInterfacePhysicObjectButton *smallMineButton = new GameInterfacePhysicObjectButton( mLayer, this, BOMB_SMALL_MINE, ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.7f ), PRIZE_ARRAY[ BOMB_SMALL_MINE ] );
	mObjectButtons.push_back( smallMineButton );
	// medium mine
	GameInterfacePhysicObjectButton *mediumMineButton = new GameInterfacePhysicObjectButton( mLayer, this, BOMB_MEDIUM, ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.52f ), PRIZE_ARRAY[ BOMB_MEDIUM ] );
	mObjectButtons.push_back( mediumMineButton );
	// border
	GameInterfacePhysicObjectButton *woodBorderButton = new GameInterfacePhysicObjectButton( mLayer, this, BORDER_WOOD, ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.34f ), PRIZE_ARRAY[ BORDER_WOOD ] );
	mObjectButtons.push_back( woodBorderButton );

	// create money label
	mCurrentMoneyLabel = cocos2d::CCLabelBMFont::labelWithString( "0", getResource( Fonts::FONT_NORMAL ) );
	mCurrentMoneyLabel->setScaleX( RATEX * 0.55f );
	mCurrentMoneyLabel->setScaleY( RATEY * 0.55f );
	mCurrentMoneyLabel->setPosition( ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.835f ) );
	mLayer->addChild( mCurrentMoneyLabel, Z_LAYER_INTERFACE );

	// add header label (name of the current level without .lua extension)
	std::string levelString( mGameScene->getCurrentLevel() );
	levelString = levelString.substr( 0, levelString.find_first_of( "." ) );
	size_t found_slash;
	found_slash = levelString.find( "/" );
	if ( found_slash != string::npos )
	{
		levelString = levelString.substr( ( levelString.find_first_of( "/" ) + 1 ), levelString.length() - ( levelString.substr( 0, levelString.find_first_of( "/" ) ).length() ) );
	}
	cocos2d::CCLabelBMFont *headerLabel = CCLabelBMFont::labelWithString( levelString.c_str(), getResource( Fonts::FONT_NORMAL ) );
	headerLabel->setScaleX( RATEX * 0.8f );
	headerLabel->setScaleY( RATEY * 0.8f );
	headerLabel->setPosition( ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.925f ) );
	mLayer->addChild( headerLabel, Z_LAYER_INTERFACE );

	// detonate button
	CCLabelBMFont *detoLabel1 = CCLabelBMFont::labelWithString( "Detonate!", getResource( Fonts::FONT_NORMAL ) );
	CCMenuItemLabel *detoLabel = CCMenuItemLabel::itemWithLabel( detoLabel1, this, menu_selector( GameInterfaceGameContainer::detonateCallback ) );
	detoLabel->setPosition( WIN_WIDTH * 0.905f, WIN_HEIGHT * 0.18f );
	detoLabel->setScale( RATEX * 0.57f );
	mMenu->addChild( detoLabel, Z_LAYER_INTERFACE );
}

// default destructor
GameInterfaceGameContainer::~GameInterfaceGameContainer() {

}

// update function of the object
void GameInterfaceGameContainer::update( cocos2d::ccTime dt ) {
	if( mShouldCausePhysicsExplosion ) {
		// cause physics explosions
		GameWorldModule::explodeAvailableBombs( mGameScene->getGameLayer()->getGameEngine()->getPhysicObjectEngine()->getPhysicObjects() );
		mShouldCausePhysicsExplosion = false;
	}

	// TODO: remove from update function and only update when necessary
	updateMoneyLabel();
}

// help button
void GameInterfaceGameContainer::helpCallback( cocos2d::CCObject *pSender ) {

}

// detonate callback
void GameInterfaceGameContainer::detonateCallback( cocos2d::CCObject *pSender ) {
	// cause physics explosions
	mShouldCausePhysicsExplosion = true;
}

// create and return the physics body of the wanted element
b2Body *GameInterfaceGameContainer::createNewItem( GameCamera *camera, PhysicObjectItems type, b2Vec2 pos ) {
	// for creating bombs
	if( type < BOMB_INVALID )
		return mGameScene->getGameLayer()->getGameEngine()->getPhysicObjectEngine()->createBombObject( 
			mGameScene->getGameLayer()->getGameEngine()->getGameWorld(), 
			mGameScene->getGameLayer(), camera, type, pos )->getBodyObjects()->at( 0 );
	// for protection
	else
		return mGameScene->getGameLayer()->getGameEngine()->getPhysicObjectEngine()->createBorderObject( 
			mGameScene->getGameLayer()->getGameEngine()->getGameWorld(), 
			mGameScene->getGameLayer(), camera, type, pos )->getBodyObjects()->at( 0 );
}

// update money label
void GameInterfaceGameContainer::updateMoneyLabel() {
	char money[ 16 ];
	sprintf( money, "%d$", mGameScene->getGameLayer()->getGameEngine()->getCurrentMoney() );
	mCurrentMoneyLabel->setString( money );
}
